﻿using Emgu.CV;
using Scaffold;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GridCut
{
	public partial class Setting : Form
	{
		List<Point> pts;
		PCIBoard pci;
		int cnt;
		public Setting()
		{
			InitializeComponent();
			pts = new List<Point>() { Config.config.Pt1, Config.config.Pt2, Config.config.Pt3, Config.config.Pt4 };
			cnt = 4;
			tbXMCPerPX.DataBindings.Add(new Binding("Text", Config.config, "XMCPerPX", true));
			tbYMCPerPX.DataBindings.Add(new Binding("Text", Config.config, "YMCPerPX", true));
			tbVHMMax.DataBindings.Add(new Binding("Text", Config.config, "HMVMax", true));
			tbVHMMin.DataBindings.Add(new Binding("Text", Config.config, "HMVMin", true));
			tbHMTacc.DataBindings.Add(new Binding("Text", Config.config, "HMTacc", true));

			tbCTVMax.DataBindings.Add(new Binding("Text", Config.config, "CTVMax", true));
			tbCTVMin.DataBindings.Add(new Binding("Text", Config.config, "CTVMin", true));
			tbCTTacc.DataBindings.Add(new Binding("Text", Config.config, "CTTacc", true));

			tbSPVMax.DataBindings.Add(new Binding("Text", Config.config, "SPVMax", true));
			tbSPVMin.DataBindings.Add(new Binding("Text", Config.config, "SPVMin", true));
			tbSPTacc.DataBindings.Add(new Binding("Text", Config.config, "SPTacc", true));

			tbEVMax.DataBindings.Add(new Binding("Text", Config.config, "EVMax", true));
			tbEVMin.DataBindings.Add(new Binding("Text", Config.config, "EVMin", true));
			tbETacc.DataBindings.Add(new Binding("Text", Config.config, "ETacc", true));
			tbKnfThk.DataBindings.Add(new Binding("Text", Config.config, "KnfThk", true));
			tbExtraX.DataBindings.Add(new Binding("Text", Config.config, "ExtraX", true));
			tbExtraY.DataBindings.Add(new Binding("Text", Config.config, "ExtraY", true));
			tbPXpMM.DataBindings.Add(new Binding("Text", Config.config, "PXpMM", true));
			tbChessWid.DataBindings.Add(new Binding("Text", Config.config, "ChessWid", true));
			tbChessHei.DataBindings.Add(new Binding("Text", Config.config, "ChessHei", true));
			tbSlowSpeed.DataBindings.Add(new Binding("Text", Config.config, "SlowSpeed", true));
			tbSlowTime.DataBindings.Add(new Binding("Text", Config.config, "SlowTime", true));
			tbImgHei.DataBindings.Add(new Binding("Text", Config.config, "ImgHei", true));
			tbImgWid.DataBindings.Add(new Binding("Text", Config.config, "ImgWid", true));

			tbCuttingHeight.DataBindings.Add(new Binding("Text", Config.config, "CuttingHeight", true));
			tbCuttingWidth.DataBindings.Add(new Binding("Text", Config.config, "CuttingWidth", true));
			tbCutCount.DataBindings.Add(new Binding("Text", Config.config, "CutCount", true));
			tbTotalTime.DataBindings.Add(new Binding("Text", Config.config, "TotalTime", true));
			tbTimePerCut.DataBindings.Add(new Binding("Text", Config.config, "TimePerCut", true));

			var x = new Binding("Text", Config.config, "Offset", true);
			x.Parse += (sender, e) => { e.Value = new PointConverter().ConvertFromString(e.Value.ToString()); };
			x.Format += (sender, e) => { e.Value = new PointConverter().ConvertToString(e.Value); };
			tbOffset.DataBindings.Add(x);


			x = new Binding("Text", Config.config, "OffsetAfterSpin", true);
			x.Parse += (sender, e) => { e.Value = new PointConverter().ConvertFromString(e.Value.ToString()); };
			x.Format += (sender, e) => { e.Value = new PointConverter().ConvertToString(e.Value); };
			tbOffsetAfterSpin.DataBindings.Add(x);

			x = new Binding("Text", Config.config, "StartPoint", true);
			x.Parse += (sender, e) => { e.Value = new PointConverter().ConvertFromString(e.Value.ToString()); };
			x.Format += (sender, e) => { e.Value = new PointConverter().ConvertToString(e.Value); };
			tbStartPoint.DataBindings.Add(x);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (Config.config.Save())
			{
				DialogResult = DialogResult.OK;
				Close();
			}
			else
			{
				MessageBox.Show("保存文件失败");
			}
		}

		//选择图像
		private void btnSelectImg_Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					Mat t = new Mat();
					Form1.ip.calibrator.Undistort(CvInvoke.Imread(openFileDialog1.FileName), t);
					Config.config.ImgLocation = openFileDialog1.FileName;
					imageBox1.Image = t;
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
					Console.WriteLine("相机未校正!!");
				}
			}
		}

		private void btnRecalpers_Click(object sender, EventArgs e)
		{
			imageBox1.MouseClick += ImageBox1_MouseClick;
			cnt = 0;
		}

		//点击选择参考点
		private void ImageBox1_MouseClick(object sender, MouseEventArgs e)
		{
			pts[cnt++] = Utils.Convert2ImagePt( e.Location, imageBox1 );//原图上的位置
			if (cnt == 4)
			{
				Config.config.Pt1 = pts[0]; Config.config.Pt2 = pts[1];
				Config.config.Pt3 = pts[2]; Config.config.Pt4 = pts[3];
				imageBox1.MouseClick -= ImageBox1_MouseClick;
				imageBox1.Image = Form1.ip.WarpPerspective(imageBox1.Image);
			}
		}
		private void btnTestTime_Click(object sender, EventArgs e)
		{
			new Task(() =>
			{
				pci = Form1.pci;
				pci.Output(3, true); pci.Output(5, true);
				int tm = pci.WaitForInputState(3, true);
				pci.Output(3, false);
				Thread.Sleep(1000); pci.Output(5, false);
				MessageBox.Show(this, $"下降时间为{tm}毫秒");

				pci.Output(2, true); pci.Output(5, true);
				pci.WaitForInputState(2, true);
				pci.Output(2, false);
				Thread.Sleep(1000); pci.Output(5, false);
			}).Start();
		
		}
	}
}
